package com.pda.common.pages.outscanf;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pda.common.R;
import com.pda.common.bean.BillBean;
import com.pda.common.bean.MessageWarp;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OutScanfAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;
    List<BillBean.DataBean> mlists = new ArrayList<BillBean.DataBean>();
    private int selectedPosition = -1;// 选中的位置

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public OutScanfAdapter(Context context, List<BillBean.DataBean> lists) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_outscanf, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.tv1.setText("产品编号:" + mlists.get(position).getProductCode());
        viewHolder.tv2.setText("产品名称:" + mlists.get(position).getProductName());
        viewHolder.tv3.setText("应扫数量:" + mlists.get(position).getYScanNums() + "");
        viewHolder.tv4.setText("实扫数量:" + mlists.get(position).getSScanNums() + "");
        viewHolder.iv.setChecked(mlists.get(position).isCk());
        viewHolder.iv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                MessageWarp messageWarp = new MessageWarp();
                messageWarp.setCode(position);
                messageWarp.setFlag(isChecked);
                EventBus.getDefault().post(messageWarp);
            }
        });
        return convertView;
    }


    static class ViewHolder {
        @BindView(R.id.iv)
        CheckBox iv;
        @BindView(R.id.tv1)
        TextView tv1;
        @BindView(R.id.tv2)
        TextView tv2;
        @BindView(R.id.tv3)
        TextView tv3;
        @BindView(R.id.tv4)
        TextView tv4;
        @BindView(R.id.ll)
        RelativeLayout ll;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

